#include <MAlice/MAliceDriver.hpp>

#include <MAlice/Ast/StringLiteralAst.h>

#include <MAlice/Ast/TypeAst.h>
#include <MAlice/Ast/PointerTypeAst.h>
#include <MAlice/Ast/ListAst.h>
#include <MAlice/Ast/FunctionProtoAst.h>
#include <MAlice/Ast/FunctionParameterAst.h>
#include <MAlice/Ast/FunctionDeclAst.h>

MAliceDriver::MAliceDriver(const std::string& filename) :
	mFilename(filename), mExpr(new MAlice::ListAst(location())), mFunc()
{
}

MAliceDriver::~MAliceDriver()
{
}

MAliceDriver* MAliceDriver::newExpression(MAlice::Ast* expr)
{
	mExpr->adopt(expr);
	return this;
}

MAliceDriver* MAliceDriver::newFunction(MAlice::Ast* func)
{
	mFunc.push_back(func);
	return this;
}

string& MAliceDriver::getFileName()
{
	return mFilename;
}

MAlice::ListAst* MAliceDriver::get()
{
	using namespace MAlice;
	location yyloc = location();

	ListAst* params = new ListAst(yyloc);
	params->adopt(new FunctionParameterAst(yyloc , new TypeAst(yyloc , "number"), "argc"));
	params->adopt(new FunctionParameterAst(yyloc , new PointerTypeAst(yyloc , "sentence"), "argv"));

	FunctionProtoAst* proto = new FunctionProtoAst(yyloc , "main", new TypeAst(yyloc , "number"), params);
	FunctionDeclAst* mainFunc = new FunctionDeclAst(yyloc , proto, mExpr);

	ListAst* ast = new ListAst(yyloc);

	for(vector<Ast*>::iterator i = mFunc.begin(), e = mFunc.end(); i != e; i++)
	{
		ast->adopt(*i);
	}

	ast->adopt(mainFunc);


	return ast;
}
